package ch.zbw.sysventory.agent.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.io.FileUtils;

public class AgentPropertiesUtil {

	private static final String PROPERTIES_FILE_NAME = "agent.properties";
	private static AgentPropertiesUtil instance;

	private Properties properties;

	private AgentPropertiesUtil() {
		this.properties = new Properties();
	}

	public static AgentPropertiesUtil getInstance() throws Exception {
		if (instance == null) {
			instance = new AgentPropertiesUtil();
			instance.load();
		}
		return instance;
	}

	public void load() throws Exception {
		File propertiesFile = new File(PROPERTIES_FILE_NAME);
		InputStream is = FileUtils.openInputStream(propertiesFile);
		this.properties.load(is);
		is.close();
	}

	public void save() throws Exception {
		FileOutputStream out = new FileOutputStream(new File(PROPERTIES_FILE_NAME));
		this.properties.store(out, "");
		out.close();
	}

	public Properties getProperties() {
		return this.properties;
	}

}
